import { connect } from 'dva'

import { hospitalAndAreaTreeApi } from '@/api/pcs/pcsHospital'
import AekButton from '@/components/AekButton'
import AekLinkButton from '@/components/AekLinkButton'
import AekTable from '@/components/AekTable'
import Ellipsis from '@/components/Ellipsis'
import PageLayout from '@/components/PageLayout'
import SearchForm from '@/components/SearchForm'
import { namespace } from '@/models/pcs/pcsCostStatistics'
import { getPagination, message } from '@/utils/antdHelper'
import { formatMoneyNum } from '@/utils/displayHelper'
import { getBasicFn } from '@/utils/dvaUtil'
import { PAY_CHANNEL, PAY_STATUS } from '@/utils/enum'

import { PcsStatementDetailDrawer } from '../pcsStatementManage/pcsStatementDetail'

import HandleTransactionModal from './components/HandleTransactionModal'

/** 陪护 - 费用统计 */
const Index = ({ modelState, loading }) => {
  const { toAction, getLoading } = getBasicFn({ namespace, loading })
  const { pageList = [], pagination, searchParams, totalObj } = modelState

  /** 处理 */
  const handleTransaction = async row => {
    await HandleTransactionModal.PromiseThen({
      row,
    })
    toAction('getPageList')
  }

  /** 导出 */
  const transportUsersStatisticsExport = async () => {
    // 列表长度大于0才导出
    if (!pageList.length) {
      message.warning('暂无数据')
      return
    }
    toAction('exportFile')
  }

  /** @type {import("@/components/SearchForm").SearchFormProps} */
  const searchProps = {
    searchParams,
    formData: [
      { field: 'nursingWorkerName', label: '护工姓名', component: { name: 'Input' } },
      { field: 'account', label: '银行账户', component: { name: 'Input' } },
      {
        field: 'status',
        label: '支付状态',
        component: {
          name: 'Select',
          props: { options: PAY_STATUS.options },
        },
      },
      {
        field: 'channel',
        label: '支付渠道',
        component: {
          name: 'Select',
          props: { options: PAY_CHANNEL.options },
        },
      },
      {
        field: 'areaIdList',
        label: '医院院区',
        component: {
          name: 'AekCascader',
          props: {
            api: hospitalAndAreaTreeApi,
            fieldNames: {
              children: 'areaInfos',
            },
          },
        },
      },
      { field: 'statementNo', label: '结算单号', component: { name: 'Input' } },
      { field: 'billNo', label: '账单号码', component: { name: 'Input' } },
      { field: 'orderNo', label: '订单号码', component: { name: 'Input' } },
      { field: 'payDateRange', label: '支付日期', component: { name: 'RangePicker' } },
    ],
    onSearch: values => {
      toAction({ searchParams: values })
      toAction({ pagination: { current: 1 } }, true)
      toAction('getPageList')
    },
  }

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: namespace,
    rowKey: 'id',
    loading: getLoading('getPageList'),
    dataSource: pageList,
    pagination: getPagination(pagination, (current, pageSize) => {
      toAction('getPageList', { current, pageSize })
    }),
    beforePagination: (
      <span>
        <span className='mr40px'>
          合计账单费用: {<span className='c-money'>{formatMoneyNum(totalObj.totalBillAmount)}</span>}
        </span>
        <span className='mr40px'>
          合计支付手续费用: {<span className='c-money'>{formatMoneyNum(totalObj.totalPayCommission)}</span>}
        </span>
        <span className='mr40px'>
          合计护工费用: {<span className='c-money'>{formatMoneyNum(totalObj.totalNursingAmount)}</span>}
        </span>
        <span className='mr40px'>
          合计应缴服务费: {<span className='c-money'>{formatMoneyNum(totalObj.totalServiceCharge)}</span>}
        </span>
        <span className='mr40px'>
          合计管理费用: {<span className='c-money'>{formatMoneyNum(totalObj.totalManageFee)}</span>}
        </span>
      </span>
    ),
    columns: [
      { dataIndex: 'nursingWorkerName', title: '护工姓名' },
      { dataIndex: 'nursingWorkerAccount', title: '银行账户' },
      { dataIndex: 'status', title: '支付状态', enum: PAY_STATUS },
      { dataIndex: 'channel', title: '支付渠道', enum: PAY_CHANNEL },
      { dataIndex: 'remark', title: '支付备注' },
      { dataIndex: 'billTotalAmount', title: '账单费用', isMoney: true },
      { dataIndex: 'payCommission', title: '支付手续费', isMoney: true },
      { dataIndex: 'nursingAmount', title: '护工费用', isMoney: true },
      { dataIndex: 'serviceCharge', title: '应缴服务费', isMoney: true },
      { dataIndex: 'manageFee', title: '管理费用', isMoney: true },
      {
        dataIndex: 'statementNo',
        title: '结算单号',
        render: (text, row) => (
          <AekLinkButton
            onClick={() => {
              PcsStatementDetailDrawer.PromiseThen({
                id: row.statementId,
              })
            }}
          >
            {text}
          </AekLinkButton>
        ),
      },
      { dataIndex: 'payUserName', title: '支付人' },
      { dataIndex: 'payTime', title: '支付时间', isDateTime: true },
      { dataIndex: 'dealUserName', title: '处理人' },
      { dataIndex: 'dealTime', title: '处理时间', isDateTime: true },
      { dataIndex: 'dealResult', title: '处理结果' },
      {
        dataIndex: 'dealFileList',
        title: '附件',
        render: text => {
          if (!text) return '-'
          return (
            <div>
              {text.map((item, index) => (
                <a key={index} href={item.fileUrl} target='_blank' download={item.fileUrl}>
                  <Ellipsis>{item.fileName}</Ellipsis>
                </a>
              ))}
            </div>
          )
        },
      },

      {
        key: 'operate',
        title: '操作',
        fixed: 'right',
        width: 100,
        className: 'text-center!',
        render: (_, row) => (
          <>
            {row.status === PAY_STATUS.FAIL && (
              <AekLinkButton onClick={() => handleTransaction(row)}>处理</AekLinkButton>
            )}
          </>
        ),
      },
    ],
  }

  return (
    <PageLayout
      extraSuffix={
        <AekButton type='primary' onSubmit={transportUsersStatisticsExport}>
          导出
        </AekButton>
      }
    >
      <SearchForm {...searchProps} />
      <AekTable {...tableProps} />
    </PageLayout>
  )
}
export default connect(store => ({
  modelState: store[namespace],
  loading: store.loading,
}))(Index)
